import VueRouter from "vue-router";
import Message from "element-ui/packages/message/src/main";

let router = new VueRouter({
    routes:[
        {path: "/", redirect:'/UserIndex'},
        // {path: "/", redirect:'/welcome'},
        {path: '/welcome', meta:{index:2,title: '弘仁族谱管理系统'}, component: () => import('@/page/welcome')},
        {path: '/register', meta:{index:2,title: '注册账号'}, component: () => import('@/page/register')},




        {
            path: 'user',
            component: () => import('@/userpage/UserLayout'),
            children: [
                {path: '/UserIndex', meta:{index:2,title: '欢迎使用弘仁族谱'}, component: () => import('@/userpage/UserIndex')},
                {path: '/FamilyInformation', meta:{index:2,title: '家族资讯'}, component: () => import('@/userpage/FamilyInformation')},
                {path: '/LookGenealogys', meta:{index:2,title: '查阅族谱'}, component: () => import('@/userpage/LookGenealogys')},
                {path: '/messages', meta:{index:2,title: '论坛留言'}, component: () => import('@/userpage/messages')},
                {path: '/HelpCenter', meta:{index:2,title: '帮助中心'}, component: () => import('@/userpage/HelpCenter')},
            ]
        },


        {
            path: 'welcome',
            component: () => import('@/page/welcome'),
            children: [
                {path: '/login', meta:{index:1,title: '登录账号'}, component: () => import('@/page/login')},
            ]
        },

        {
            path: 'admin',
            component: () => import('@/page/layout'),
            children:[
                {path: '/index', meta:{index:3,title: '弘仁族谱管理系统'}, component: () => import('@/page/index')},
                {path: '/message', meta:{index:4,title: '论坛留言'},  component: () => import('@/page/message')},
                {path: '/ContactAuthor', meta:{index:5,title: '联系作者'},  component: () => import('@/page/ContactAuthor')},
                {path: '/PersonalInformation', meta:{index:6,title: '个人中心'},  component: () => import('@/page/PersonalInformation')},
                {path: '/FamilyOrigin', meta:{index:7,title: '资讯管理'},  component: () => import('@/functionpage/FamilyOrigin')},
                {path: '/AddNews', meta:{index:11,title: '添加资讯'},  component: () => import('@/functionpage/AddNews')},
                {path: '/LookGenealogy',meta:{index:8,title: '查阅族谱'},  component: () => import('@/functionpage/LookGenealogy')},
                {path: '/ManagementGenealogy', meta:{index:9,title: '管理族谱'},  component: () => import('@/functionpage/ManagementGenealogy')},
                {path: '/AddFamilyAccount', meta:{index:10,title: '添加族员账号'},  component: () => import('@/functionpage/AddFamilyAccount')},
            ]
        },




    ],
    // 设置mode为history，可以去掉路径上的/#/号
    mode:'history'
})

//不需要登录的路径【白名单】
let notNeedLogin = ['/welcome','/login', '/register','/UserIndex'];

//每次路由跳转之前执行
router.beforeEach(((to , from , next ) => {
    if (notNeedLogin.indexOf(to.path) == -1){
        //判断TOKEN是否为空
        if (localStorage.getItem('TOKEN') == null ){
            Message.warning('请先登录！');
            //拦截并跳转到指定页面
            next('/UserIndex');
        }
    }
    //TOKEN不为空则放行
    next();
}))

export default router;
